package com.android.fragmentdemo.roomdb
import android.text.TextUtils
import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

object PetConverter {
    @JvmStatic
    @TypeConverter
    fun fromList(petList: MutableList<Pet>): String {
        return Gson().toJson(petList)
    }

    @JvmStatic
    @TypeConverter
    fun toList(value: String): MutableList<Pet> {
        if (TextUtils.isEmpty(value)) {
            return mutableListOf()
        } else {
            val listType = object : TypeToken<MutableList<Pet>>() {}.type
            return Gson().fromJson(value, listType)
        }
    }
}